/*****************************************************************************
 *   Copyright 2005 Tim A Wang
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 ****************************************************************************/
package com.jb.web.renderer;

import com.jb.faces.context.FacesContext;
import com.jb.faces.render.RenderKit;
import com.jb.faces.render.RenderKitFactory;
import com.jb.framework.Registry;
import com.jb.util.ClassUtil;
import com.jb.web.bean.HtmlItem;
import com.jb.web.bean.UIComponentImpl;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;


/**
 *
 */
public class RenderKitFactoryImpl extends RenderKitFactory {
    HashMap renderKits = new HashMap();
    private Registry rendererCache = new Registry() {
            public boolean testRegistration(Object pRegistered, Object pTestObj) {
                if (!(pRegistered instanceof Class)) {
                    return false;
                }

                return pRegistered.equals(pTestObj);
            }
        };

    private Registry rendererRegistiry = new Registry() {
            public boolean testRegistration(Object pRegistered, Object pTestObj) {
                if (!(pRegistered instanceof Class)) {
                    return false;
                }

                return pRegistered.equals(pTestObj);
            }
        };

    {
    }

    public RenderKit getRenderKit(FacesContext context, String renderKitId) {
        RenderKit renderKit = (RenderKit) renderKits.get(renderKitId);

        if (renderKit == null) {
            renderKit = new RenderKitImpl();
        }

        return renderKit;
    }

    public Iterator getRenderKitIds() {
        return renderKits.keySet().iterator();
    }

    /**
     *
     *
     * @param pComponent parameter
     *
     * @return return-value
     */
    public List getRenderTypes(UIComponentImpl pComponent) {
        return rendererRegistiry.getRegisteredValues(pComponent.getClass());
    }

    public RendererImpl getRenderer(HtmlItem pComponent) {
        List list = rendererCache.getRegisteredValues(pComponent.getClass());
        String renderType = pComponent.getRendererType();

        if ((list != null) && (list.size() > 0)) {
            for (int i = 0; i < list.size(); i++) {
                RendererImpl renderer = (RendererImpl) list.get(i);

                if (renderer.getClass().getName().equals(renderType)) {
                    return renderer;
                }
            }
        }

        Class renderClass = null;

        try {
            if ((renderType == null) || (renderType.length() == 0)) {
                list = rendererRegistiry.getRegisteredValues(pComponent.getClass());

                if ((list != null) && (list.size() > 0)) {
                    renderClass = (Class) list.get(0);
                    pComponent.setRendererType(renderClass.getName());
                }
            } else {
                renderClass = ClassUtil.getClass(renderType);
                if(renderClass==null)
                {
                    list = rendererRegistiry.getRegisteredValues(pComponent.getClass());

                    if ((list != null) && (list.size() > 0)) {
                        renderClass = (Class) list.get(0);
                        pComponent.setRendererType(renderClass.getName());
                    }
                }
            }

            if (renderClass != null) {
                RendererImpl renderer = (RendererImpl) renderClass.newInstance();
                rendererCache.register(pComponent.getClass(), renderer);

                return renderer;
            }
        } catch (Exception e) {
            System.err.print("Renderer " + renderType + " can not be initialized");
        }

        return new DefaultRenderer();
    }

    public void addRenderKit(java.lang.String renderKitId, RenderKit renderKit) {
        renderKits.put(renderKitId, renderKit);
    }

    /**
     *
     * @param renderType parameter
     * @param rendererClass parameter
     */
    public void registerRenderer(Class renderType, Class rendererClass) {
        rendererRegistiry.register(renderType, rendererClass);
    }
}
